/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.entity;

import java.io.Serializable;
import javax.persistence.*;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author israel
 */
@Entity
public class Zipcode implements Serializable {
    private static final long serialVersionUID = 1L;
    
    @Id
    @Column(name="hash_id")
    private Long id;
    
    @Column(unique=true)
    private int code;
    
    @ManyToOne
    @JoinColumn(name="city_id")
    private City city;
    
    @ManyToOne
    @JoinColumn(name="neighborhood_id")
    private Neighborhood neighborhood;
    
    @ManyToOne
    @JoinColumn(name="street_id")
    private Street street;

    public Zipcode() {
    }

    @PrePersist
    public void generateHash(){
        if(getId() == null){
            String zipcodeHashString = "" + this.getCode() + this.getCity().getId();
            this.setId(Long.valueOf(EncryptionUtil.encrypt(zipcodeHashString).hashCode()));
        }
    }
    
    public Zipcode(int code, City city, Neighborhood neighborhood, Street street) {
        this.code = code;
        this.city = city;
        this.neighborhood = neighborhood;
        this.street = street;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public City getCity() {
        return city;
    }

    public void setCity(City city) {
        this.city = city;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public Neighborhood getNeighborhood() {
        return neighborhood;
    }

    public void setNeighborhood(Neighborhood neighborhood) {
        this.neighborhood = neighborhood;
    }

    public Street getStreet() {
        return street;
    }

    public void setStreet(Street street) {
        this.street = street;
    }
    
}
